<?php
class ReferrersPlugin extends AbstractPlugin {
	protected $engines;
	
	/**
	 * Default constructor
	 */
	public function __construct() {
		parent::__construct();
		
		$this->engines = array(
			array('a9', '/'),
			array('alexa', 'q'),
			array('alexa', 'url'),
			array('alltheweb', 'q'),
			array('altavista', 'q'),
			array('altavista', 'backurl', 'q'),
			array('aol', 'q'),
			array('aol', 'query'),
			array('ask', 'q'),
			array('ask', 'ask'),
			array('bloglines', 'q'),
			array('dogpile', 'q'),
			array('excite', '/info.xcite/search/web/'),
			array('feedminer', 'q'),
			array('google', 'q'),
			array('googlesyndicatedsearch', 'q'),
			array('images.google', 'prev', 'q'),
			array('lycos', 'query'),
			array('hotbot', 'query'),
			array('jekoo', 'ksc'),
			array('live', 'q'),
			array('msn', 'q'),
			array('mysearch', 'searchfor'),
			array('myway', 'searchfor'),
			array('netscape', 'search'),
			array('netscape', 'query'),
			array('search', 'q'),
			array('technorati', '/posts/tag/'),
			array('technorati', '/blogs/tag/'),
			array('technorati', '/videos/tag/'),
			array('technorati', '/photos/tag/'),
			array('yahoo', 'p'),
			array('images.yahoo', 'back', 'p')
		);
	}
	
	/**
	 * Enables this plugin
	 */
	public function enable() {
		$db = Cinnamon::registry('db');
		$db->addTableColumn($db->requests, 'referrer_domain', 'VARCHAR(100)');		
		$db->addTableColumn($db->requests, 'referrer_url', 	'VARCHAR(250)');
		$db->addTableColumn($db->requests, 'referrer_title', 	'VARCHAR(250)');
		$db->addTableColumn($db->requests, 'search_terms', 	'VARCHAR(250)');
	}
	
	public function onFilters($event) {
		//Cinnamon::registry('filters')->add(
		//	new Filter('referrer', Cinnamon::registry('db')->requests, 'referrer_domain')
		//);
	}	
	
	public function onContent($evt) {
		$page = $evt->page;
		if ( $page == '/reports/traffic.php' ) {
			include_once dirname(__FILE__) .'/report.php';
		}
	}
	
	public function onJavascript($evt) {
		$evt->addParameter('referrer', 'document.referrer');
	}	
	
	public function onTrack($evt) {
		if ( $evt->referrer == null ) {	
			return;	// we dont need empty referrers
		}
		
		// avoid report site as the referrer
		$s = parse_url($evt->site_url);
		$r = parse_url($evt->referrer);
		if ( hostcmp($s['host'], $r['host']) == 0 && strcmp($s['port'], $r['port']) == 0 ) {
			return;
		}
		unset($s);
		
		// avoid report cse url as the referrer
		$s = parse_url(CSEURL);
		if ( hostcmp($s['host'], $r['host']) == 0 && strcmp($s['port'], $r['port']) == 0 && strstr($r['path'], $s['path']) == 0 ) {
			return;
		}
		unset($s);
	
	
		// add referrer domain as parameter
		$evt->addParameter('referrer_domain', $r['host']);
		unset($r);
		
		// add referrer url as parameter
		$evt->addParameter('referrer_url', $evt->referrer);
		
		// add referrer url as parameter
		Cinnamon::loadClass('URLReader');
		$r = new URLReader($evt->referrer);		
		$evt->addParameter('referrer_title', $r->getTitle());
		unset($r);
	
		// add search terms as parameter
		$evt->addParameter('search_terms', $this->getSearchTerms($evt->referrer));
	}
	
	public function getSearchTerms($url) {
		$url = parse_url($url);
		foreach ( $this->engines as $engine ) {
			if ( preg_match('/'. preg_quote($engine[0]) .'\./i', $url['host']) ) {
				// paths
				if ( substr($engine[1], 0, 1) == '/' ) {
					$terms = str_replace($engine[1], '', $url['path']);
					if ( strlen($terms) < strlen($url['path']) ) {
						$terms = urldecode($terms);
						$terms = str_replace('+', ' ', $terms);
						return $terms;
					}
				}
				// variables
				else {
					for ( $i=1; $i<count($engine); $i++ ) {
						parse_str($url['query'], $params);	
						$key = $engine[$i];
						if ( array_key_exists($key, $params) ) {
							$terms = $params[$key];
							if ( $i == count($engine)-1 ) 
								return $terms;
							else
								$url = parse_url($terms);
						}
					}		
				}
			}		
		}
		
		return null;
	}
}
?>
